home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wutilities.lzh / wSetSysRequest / wSetSysRequest.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  5KB  |  181 lines

  1. /*
  2.  *  WSETSYSREQUEST  A companion utility to wIconify that allows you to
  3.  *                  change the screen where system requesters open
  4.  *                  (designed for use with CLI's that are openned on
  5.  *                  a screen other than the WB screen).
  6.  *
  7.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  8.  *  You may use this code, provided this copyright notice is kept intact.
  9.  */
  10.  
  11. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  12. #include <intuition/intuitionbase.h>
  13. #include <libraries/dosextens.h>
  14.  
  15. #define USAGE   "wSetSysRequest [process [screen]]"
  16.  
  17. static char *program = "wSetSysRequest";
  18. static char *version = "v1.2";
  19. static char *copyright =
  20.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  21.  
  22.  
  23. #define INTUITION_REV   0L
  24. extern struct IntuitionBase *IntuitionBase;
  25. extern struct IntuitionBase *OpenLibrary();
  26. extern struct Process *FindTask();
  27. extern APTR wBackDropOf();
  28.  
  29.  
  30. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  31.  
  32.  
  33. /*
  34.  *  PrefixMatch()
  35.  *
  36.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  37.  *  only match themselves, and are not considered prefixes to any string.
  38.  *
  39.  *  Return <0 if the first is smaller than the second,
  40.  *         =0 if the first is a prefix of the second,
  41.  *         >1 if the first is larger than the second.
  42.  */
  43.  
  44. int PrefixMatch(s1,s2)
  45. char *s1,*s2;
  46. {
  47.    int match = -1;
  48.    
  49.    if (s1 && *s1)
  50.    {
  51.       if (s2)
  52.       {
  53.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  54.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  55.       } else match = 1;
  56.    } else if (s2 == NULL || *s2 == 0) match = 0;
  57.    return(match);
  58. }
  59.  
  60.  
  61. /*
  62.  *  *FindScreen()
  63.  *
  64.  *  Lock IntuitionBase so nothing happens while we look.
  65.  *  If there is a name to look for,
  66.  *    Start at the first screen, and look for a screen whose title
  67.  *      matches the given name.  If none, return NULL.
  68.  *  Otherwise, use the active screen.
  69.  *  Unlock Intuition.
  70.  *  Return the screen pointer found, if any.
  71.  */
  72.  
  73. static struct Screen *FindScreen(ScreenName)
  74. char *ScreenName;
  75. {
  76.    struct Screen *theScreen;
  77.    long ILock, LockIBase();
  78.  
  79.    ILock = LockIBase(0L);
  80.    if (ScreenName)
  81.    {
  82.       theScreen = IntuitionBase->FirstScreen;
  83.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  84.          theScreen = theScreen->NextScreen;
  85.    } else {
  86.       theScreen = IntuitionBase->ActiveScreen;
  87.    }
  88.    UnlockIBase(ILock);
  89.    return(theScreen);
  90. }
  91.  
  92.  
  93. /*
  94.  *  Print()
  95.  *
  96.  *  Find the standard AmigaDOS output file and write the output string
  97.  *  to the output file.  This is intended as a substitute for printf()
  98.  *  when no formatting is required, and when you want a small executable.
  99.  */
  100.  
  101. static void Print(s)
  102. char *s;
  103. {
  104.    ULONG OutFile;
  105.    extern ULONG Output();
  106.    
  107.    OutFile = Output();
  108.    if (OutFile && s) Write(OutFile,s,strlen(s));
  109. }
  110.  
  111.  
  112. /*
  113.  *  Error()
  114.  *
  115.  *  Print the character string, and up to two optional strings, then
  116.  *  go to a new output line, close Intuition, and exit with an error.
  117.  */
  118.  
  119. static void Error(s,x1,x2)
  120. char *s,*x1,*x2;
  121. {
  122.    Print(s);
  123.    if (x1) Print(x1);
  124.    if (x2) Print(x2);
  125.    Print("\n");
  126.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  127.    _exit(10L);
  128. }
  129.  
  130.  
  131. /*
  132.  *  main()
  133.  *
  134.  *  If there are too many arguments, give the Usage message.
  135.  *  If there is at least one argument, make it the process name.
  136.  *  If there are two arguements, make the second the screen name.
  137.  *  Find the specified process (ourself, if ProcName is NULL).
  138.  *  If found, then
  139.  *    Open Intuition.
  140.  *    Find the specified screen (the active one if NULL)
  141.  *    If wIcoinfy is running, set the process window pointer to the
  142.  *      backdrop window of the specified screen; system requests will
  143.  *      appear on the screen where this window is found.
  144.  *    Otherwise show an error message.
  145.  *    Close Intuition.
  146.  *  Otherwise show an error message.
  147.  */
  148.  
  149. void main(argc,argv)
  150. int argc;
  151. char *argv[];
  152. {
  153.    char *ProcName = NULL;
  154.    char *ScreenName = NULL;
  155.    struct Process *theProcess;
  156.    struct Screen *theScreen;
  157.  
  158.    if (argc > 3) Error("Usage:  ",USAGE,NULL);
  159.  
  160.    if (argc > 1 && argv[1]) ProcName = argv[1];
  161.    if (argc > 2 && argv[2]) ScreenName = argv[2];
  162.  
  163.    theProcess = FindTask(ProcName);
  164.    if (theProcess)
  165.    {
  166.       IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  167.       if (IntuitionBase)
  168.       {
  169.          theScreen = FindScreen(ScreenName);
  170.          if (theScreen)
  171.          {
  172.             if (wIconifyActive())
  173.               theProcess->pr_WindowPtr = wBackDropOf(theScreen);
  174.              else
  175.               Error("wIconify not running or incompatible version",NULL,NULL);
  176.          } else Error("Can't find screen '",ScreenName,"'");
  177.          CloseLibrary(IntuitionBase);
  178.       } else Error("Can't Open Intuition Library!",NULL,NULL);
  179.    } else Error("Can't Find Task '",ProcName,"'");
  180. }
  181.